#!/usr/bin/env python

# lxc-api , created 7/2014
# Create by Chris Haessig


#  Import Yaml, Flash nad custom LXC library

from yaml import safe_load
from flask import Flask, session, redirect, url_for, escape, request
app = Flask(__name__)
from lxc import lxccontrol

# Example Text

example_text = """
GET /login - Auth to server<br>
GET /list - List all the LXC containers<br>
GET /create/<type>/<name> - Create a LXC<br>
GET /destroy/<name> - Destroy LXC<br>
GET /freeze/<name> - Freeze a LXC<br>
GET /unfreeze/<name> - Unfreeze a LXC<br>
GET /snapshot/<name> - Snapshot a LXC<br>
"""

# Open Yaml file and grab values

with open("api.yaml") as f:
  y = f.read()

yam = safe_load(y)
username = yam['username']
password = yam['password']
token    = yam['token']
port    = yam['port']
host    = yam['host']

# List all LXC

@app.route('/list')
def list():
  if 'LoggedIn' in session:
    return lxccontrol("list")
  else:
    return ""


# Default, does nothing

@app.route('/')
def index():
  if 'LoggedIn' in session:
        return 'Ready...'
  return redirect(url_for('login'))

# Login to lxc-api

@app.route('/login', methods=['GET','POST'])
def login():
    if request.method == 'POST':
      if request.form['username'] == username and request.form['password']:
        session['LoggedIn'] = "yes"
        return redirect(url_for('index'))
    return '''
        <form action="#" method="post">
            <p>Username <input type="text" name="username">
            <p>Password <input type="password" name="password">
            <p><input type="submit" value="Login">
        </form><br><br>{0}
    '''.format(example_text)

# Logout

@app.route('/logout')
def logout():
    # remove the username from the session if it's there
    if 'LoggedIn' in session:
      session.pop('LoggedIn', None)
      return redirect(url_for('index'))
    else:
      return ""


# Stop LXC

@app.route('/stop/<machine>')
def delete(machine):
   if 'LoggedIn' in session:
     return lxccontrol("stop",machine)

# Start LXC

@app.route('/start/<machine>')
def start(machine):
   if 'LoggedIn' in session:
     return lxccontrol("start",machine)
   else:
     return ""

# Snapshot LXC

@app.route('/snapshot/<machine>')
def snapshot(machine):
   if 'LoggedIn' in session:
     return lxccontrol("snapshot",machine)
   else:
     return ""

# Freeze LXC

@app.route('/freeze/<machine>')
def freeze(machine):
   if 'LoggedIn' in session:
     return lxccontrol("freeze",machine)
   else:
     return ""

# Unfreeze LXC

@app.route('/unfreeze/<machine>')
def unfreeze(machine):
   if 'LoggedIn' in session:
     return lxccontrol("unfreeze",machine)
   else:
     return ""
    
# Destroy LXC

@app.route('/destroy/<machine>')
def destroy(machine):
   if 'LoggedIn' in session:
     return lxccontrol("destroy",machine)
   else:
     return ""

@app.route('/create/<types>/<machine>')
def create(types,machine):
   if 'LoggedIn' in session:
     return lxccontrol("create",machine,types)
   else:
     return ""



# Set session token

app.secret_key = token

if __name__ == '__main__':
    app.run(host,port)

